package org.ekstrabilet.web.beans;

import java.util.ArrayList;
import java.util.Random;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.ekstrabilet.game.beans.Game;
import org.ekstrabilet.ticket.beans.MacTicket;

public class TicketBean {

	private String sector;
		
	private ArrayList<MacTicket> tickets = new ArrayList<MacTicket>();
	
	public int[] getNumbers() {
		 int[] nrs = {1,2,3,4};
		 return nrs;
	}
	
	public String getSector() {
		return sector;
	}

	public void setSector(String sector) {
		this.sector = sector;
	}

	public ArrayList<String> getSectors() {
		
		GameBean gb = (GameBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("game");
		
		int r = 0;
		for(int t : gb.getTribunes())
			r+=t;
		
		ArrayList<String> signs = new ArrayList<String>();	
		int asci = 65;
				
		for(int i = 0 ; i < r ; i++) {
			String s = Character.toString(((char)asci++));
			signs.add(s);
		}

		return signs;
		
		
	}
	
	public String buy() {
		
		MacTicket ticket = new MacTicket();
		
		GameBean gb = (GameBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("game");
		Game g =(Game) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("game2");
		
		ticket.setId(new Random().nextInt(1000));
		ticket.setGame(g);
		ticket.setGameBean(gb);
		ticket.setSector(sector);
		
		tickets.add(ticket);
		
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("tickets", tickets);
		FacesContext.getCurrentInstance().addMessage("ticketForm:buy",
				new FacesMessage("Ticket has been added to your cart!"));
		
		
		return null;
		
	}
}
